package com.java.generic;

/**
 * 泛型接口
 */
public class LearnGenericInterface {
    interface GenericInterface<T> { // 泛型标识符T表示Type，此处可为任意标识符
        T setAndGetVar(T var);
    }
    
    // 接口泛型确定
    static class GenericImplements1 implements GenericInterface<String> {
        private String var;
        
        @Override
        public String setAndGetVar(String var) {
            this.var = var;
            return var;
        }
    }
    
    // 接口泛型转递
    static class GenericImplements2<T> implements GenericInterface<T> {
        private T var;
        
        @Override
        public T setAndGetVar(T var) {
            this.var = var;
            return var;
        }
    }
    
    public static void main(String[] args) {
        GenericInterface<String> genericInterface1 = new GenericImplements1();
        genericInterface1.setAndGetVar("SYX");
        // genericInterface1.setAndGetVar(20); // 需要的类型String
        
        GenericInterface<String> genericInterface2 = new GenericImplements2<>();
        genericInterface2.setAndGetVar("SYX");
        // genericInterface2.setAndGetVar(20); // 需要的类型String
    }
}
